#include "TTexture.h"

HRESULT		TTexture::Load(ID3D10Device* pDevice, TCHAR* strFilePath)
{
	HRESULT hr = S_OK;

	TCHAR szFileName[MAX_PATH] = { 0, };
	_stprintf_s( szFileName, _T("%s%s"), m_szPath.c_str(), strFilePath );	

	 // Load the Texture
	hr = D3DX10CreateShaderResourceViewFromFile( pDevice, szFileName, NULL, NULL, &m_pTextureRV, NULL );
	if( FAILED( hr ) )
		return hr;

	pDevice->PSSetShaderResources( 0, 1, &m_pTextureRV);

	// Sampler
	D3D10_SAMPLER_DESC sd;
	memset(&sd, 0, sizeof(sd)); 
  
	sd.Filter	= D3D10_FILTER_MIN_MAG_MIP_LINEAR; 
	sd.AddressU = D3D10_TEXTURE_ADDRESS_WRAP; 
	sd.AddressV = D3D10_TEXTURE_ADDRESS_WRAP; 
	sd.AddressW = D3D10_TEXTURE_ADDRESS_WRAP; 
	sd.MaxLOD	= FLT_MAX; 
	sd.MaxAnisotropy	= 16;  
	sd.ComparisonFunc	= D3D10_COMPARISON_ALWAYS; 

	pDevice->CreateSamplerState(&sd,&m_pSamplerState);
	pDevice->PSSetSamplers(0,1,&m_pSamplerState);

	m_szName = strFilePath;	
	return hr;
}
bool TTexture::Apply(ID3D10Device* pDevice)
{
	assert( pDevice );
	if( !m_pSamplerState || !m_pTextureRV )
	{
		return false;
	}
	pDevice->PSSetSamplers(0,1,&m_pSamplerState);
	pDevice->PSSetShaderResources( 0, 1, &m_pTextureRV);
	return true;
}
bool TTexture::Release()
{
	SAFE_RELEASE( m_pTextureRV );
	SAFE_RELEASE( m_pSamplerState );
	return true;
}
void TTexture::SetPath( TCHAR* pPath )
{
	m_szPath = pPath;
}
TTexture::TTexture(void)
{
	m_pSamplerState	=	NULL;
	m_pTextureRV	=	NULL;
	m_szPath = _T("");
}

TTexture::~TTexture(void)
{
	SAFE_RELEASE( m_pTextureRV );
	SAFE_RELEASE( m_pSamplerState );
}

INT TTextureMgr::Add( const TCHAR *pFileName )
{
	TCHAR szFileName[MAX_PATH];
	TCHAR Drive[MAX_PATH];
	TCHAR Dir[MAX_PATH];
	TCHAR FName[MAX_PATH];
	TCHAR Ext[MAX_PATH];	
	if( pFileName )
	{										
		_tsplitpath( pFileName, Drive, Dir, FName, Ext );						
		Ext[4]= 0;	
		memset( szFileName, 0, sizeof(TCHAR) * MAX_PATH );
		_stprintf_s( szFileName, _T("%s%s"), FName, Ext );		

		for( TemplateMapItor itor = TMap.begin(); itor != TMap.end(); itor++) 
		{
			TTexture *pPoint = (TTexture *)(*itor).second;
			if( !_tcsicmp( pPoint->m_szName.c_str(), szFileName ) ) 
			{
				return (*itor).first;
			}
		}
	}
	TTexture *pPoint = NULL;
	SAFE_NEW( pPoint, TTexture );
	
	TCHAR szPath[MAX_PATH] = {0,};
	_stprintf_s( szPath, _T("%s%s"), Drive, Dir );
	pPoint->SetPath(szPath);

	if( FAILED( pPoint->Load( m_pd3dDevice, szFileName ) ))
	{
		return 0;
	}
	TMap.insert( make_pair( ++m_iCurIndex, pPoint ) );	
	return m_iCurIndex;
}
bool TTextureMgr::Release()
{
	TTexture *pPoint;
	for (TemplateMapItor itor = TMap.begin(); itor != TMap.end(); itor++ ) 
	{
		pPoint = (TTexture *)(*itor).second;
		pPoint->Release();				
	}
	TMap.clear();
	return true;
}
TTexture* TTextureMgr::GetPtr( INT iIndex )
{
	TemplateMapItor itor = TMap.find(iIndex);
	if (itor == TMap.end()) return NULL;
	TTexture *pPoint = (*itor).second;	
	return pPoint;
}
TTexture* TTextureMgr::GetPtr( TCHAR_STRING strFindName )
{		
	for( TemplateMapItor itor = TMap.begin(); itor != TMap.end(); itor++) 
	{
		TTexture *pPoint = (TTexture *)(*itor).second;
		if( pPoint->m_szName == strFindName ) 
		{
			return (*itor).second;
		}
	}
	return NULL;
}
void TTextureMgr::SetDevice(ID3D10Device*	 pDevice ) 
{
	m_pd3dDevice = pDevice;
}
TTextureMgr::TTextureMgr(void)
{
	m_pd3dDevice	= NULL;
	m_iCurIndex = 0;	
	TMap.clear();
}

TTextureMgr::~TTextureMgr(void)
{
	TTexture *pPoint;
	for( TemplateMapItor itor = TMap.begin(); itor != TMap.end(); itor++ ) 
	{
		pPoint = (TTexture *)(*itor).second;
		pPoint->Release();
		SAFE_DEL(pPoint);
	}
	TMap.clear();
	m_iCurIndex = 0;	
}
